﻿using AutoMapper;
using Gecko.Entities;
using Gecko.Models;
using Gecko.Models.Dto;
using Gecko.Respositories;
using Gecko.Util;
using log4net;
using NHibernate;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gecko.Services
{
    public class UserInfoService : IDisposable
    {
        ILog _log = LogManager.GetLogger("Gecko.Services");
        private ISession _session;
        private UserInfoRepository _userInfoRepository;
        public UserInfoService()
        {
            _session = NHibernateHelper.OpenSession();
            _userInfoRepository = new UserInfoRepository(_session);
        }

        public ServiceResponse<IList<UserInfoDto>> GetAllUser()
        {
            var response = new ServiceResponse<IList<UserInfoDto>>();
            
            try
            {

                
                IList<UserInfo> userInfo = _userInfoRepository.GetAll();
               
                response.Data = Mapper.Map<IList<UserInfoDto>>(userInfo);
                response.Message = "Success";
                response.Page = 1;
                response.PageSize = 100;
                response.Status = true;
                response.Total = userInfo.Count;
            }
            catch (Exception ex)
            {
                
                response.Status = false;
                response.Message = ex.Message;
                _log.Error(ex.Message, ex);
            }
            return response;
        }

        public ServiceResponse<UserInfoDto> AddUserInfo(UserInfoDto userInfoDto)
        {
            var response = new ServiceResponse<UserInfoDto>();
            var tx = _session.BeginTransaction();
            try
            {
                var regisUser = _userInfoRepository.GetByUserId(userInfoDto.Username);
                if (regisUser == null)
                {
                    UserInfo userInfo = Mapper.Map<UserInfo>(userInfoDto);
                    _userInfoRepository.Add(userInfo);
                    tx.Commit();
                    response.Data = Mapper.Map<UserInfoDto>(userInfo);
                    response.Message = "Insert Success";
                    
                }
                else
                { 
                    if (!String.IsNullOrEmpty(userInfoDto.Image))
                    {
                        regisUser.Image = userInfoDto.Image;
                    }
                    if (!String.IsNullOrEmpty(userInfoDto.Image))
                    {
                        regisUser.Name = userInfoDto.Name;
                    }
                    _userInfoRepository.Update(regisUser);
                    tx.Commit();

                    response.Data = Mapper.Map<UserInfoDto>(regisUser);
                    response.Message = "User exist";
                }
                response.Page = 1;
                response.PageSize = 100;
                response.Status = true;
                response.Total = 1;
            }
            catch (Exception ex)
            {
                if (tx.IsActive)
                {
                    tx.Rollback();
                }
                response.Status = false;
                response.Message = ex.Message;
                _log.Error(ex.Message, ex);
            }
            return response;
        }

        public void Dispose()
        {
            if (_session != null)
            {
                _session.Close();
            }
        }

        //Id in system
        public ServiceResponse<UserInfoDto> GetById(int id)
        {
            var response = new ServiceResponse<UserInfoDto>();
            try
            {
                response.Data = Mapper.Map<UserInfoDto>(_userInfoRepository.Get(id));
                response.Message = "success";
                response.Status = true;
            }
            catch (Exception ex)
            {
                response.Status = false;
                response.Message = ex.Message;
                _log.Error(ex.Message, ex);
            }
            return response;
        }

        public ServiceResponse<UserInfoDto> VerifyUser(String userId)
        {
            var response = new ServiceResponse<UserInfoDto>();
            try
            {
                var userInfo = _userInfoRepository.GetByUserId(userId);
                if (userInfo != null)
                {
                    response.Data = Mapper.Map<UserInfoDto>(userInfo);
                    response.Message = "Success";
                    response.Page = 1;
                    response.PageSize = 100;
                    response.Status = true;
                    response.Total = 1;
                }
                else
                {
                    throw new Exception("Not Found UserInfo in System");
                }
            }
            catch (Exception ex)
            {
                response.Status = false;
                response.Message = ex.Message;
                _log.Error(ex.Message, ex);
            }
            return response;
        }
    }
}